package com.custom.base.util

import android.os.Handler
import android.os.Looper

/**
 * HandlerUtil
 */
object SDHandlerUtil {

    private val mHandler = Handler(Looper.getMainLooper())

    fun removeCallbacks(r: Runnable) {
        synchronized(mHandler) {
            mHandler.removeCallbacks(r)
        }
    }

    //在UI线程上运行
    fun runOnUiThread(r: Runnable) {
        synchronized(mHandler) {
            mHandler.post(r)
        }
    }

    //在队列的UI线程前面运行
    fun runOnUiThreadFrontOfQueue(r: Runnable) {
        synchronized(mHandler) {
            mHandler.postAtFrontOfQueue(r)
        }
    }
    fun runOnUiThreadAtTime(r: Runnable, msgObj: Any? = null, uptimeMillis: Long) {
        synchronized(mHandler) {
            if(msgObj == null){
                mHandler.postAtTime(r, uptimeMillis)
            }else{
                mHandler.postAtTime(r, msgObj, uptimeMillis)
            }
        }
    }

    fun runOnUiThreadDelayed(r: Runnable, delayMillis: Long) {
        synchronized(mHandler) {
            mHandler.postDelayed(r, delayMillis)
        }
    }

}
